"""
文件操作：
打开
关闭
读
写
"""
import os

# ===========================文件读取====================================
# open打开或创建新文件, r只读，w写入，a追加写入
# f = open("./python.txt", "r", encoding="UTF-8")
# print("文件类型：", type(f))

# 读取所有内容
# content = f.read()
# print(content)

# 按字节数读取
# content = f.read(10)
# print(content)

# 读取全部行,获取所有行，形成一个list
# content = f.readlines()
# print(content)

# 按单行读取
# content = f.readline()
# print(content)

# 按行循环读取
# for line in f:
#     print(line)

# 关闭文件,并会刷新内容到硬盘中
# f.close()

# 此方法自动关闭文件
# with open("./python.txt", "r", encoding="UTF-8") as f:
#     for line in f:
#         print(line)

# ===============================文件写入==============================
# 覆盖写入 "w"
# with open("./python.txt", "w", encoding="UTF-8") as f:
#     # 将数据写入内存
#     f.write("AAAA")
#     f.write("BBBB")
#
#     # 将内容从缓冲区刷到硬盘，也可以不写，close的时候会自动调用flush刷到硬盘
#     f.flush()

# ===============================文件追加==============================

# with open("./python.txt", "a", encoding="UTF-8") as f:
#     # 换行写入
#     f.write("\nnihao")

